package com.tledu.production.controller.device;


import cn.hutool.json.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tledu.production.core.result.AjaxResult;
import com.tledu.production.model.device.DeviceType;
import com.tledu.production.service.device.DeviceTypeService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/device")
@AllArgsConstructor
public class DeviceTypeController {


    private final DeviceTypeService deviceTypeService;

    //分页
    @GetMapping("/device/type/list")
    @ResponseBody
    public AjaxResult<PageInfo<DeviceType>> DeviceTypeList(Integer pageNum, Integer pageSize){
        if (pageNum == null) {pageNum = 1;}
        if (pageSize == null) {pageSize = 5;}
        // 下次请求会带上分页数据
        PageHelper.startPage(pageNum, pageSize);
        List<DeviceType> list = deviceTypeService.list();
        // 通过这个api获取pageInfo
        PageInfo<DeviceType> page = PageInfo.of(list);
//        throw new CustomException("测试页面报错");
        return new AjaxResult<>(page);
    }


    //添加
    @PostMapping("/device/type/add")
    @ResponseBody
    public AjaxResult<Integer> DeviceTypeAdd(DeviceType deviceType) {

        return new AjaxResult<>(deviceTypeService.insert(deviceType));
    }

    //删除
    @GetMapping("/device/type/delete")
    @ResponseBody
    public JSONObject delete(String deviceTypeId) {
        int delete = deviceTypeService.delete(deviceTypeId);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("success", delete);
        return jsonObject;
    }


    //更新
    @PostMapping("/device/type/update")
    @ResponseBody
    public AjaxResult<Integer> DeviceTypeUpdate(DeviceType deviceType) {

        return new AjaxResult<>(deviceTypeService.update(deviceType));
    }

    //按不同字段搜索
    @GetMapping("/device/type/search")
    @ResponseBody
    public AjaxResult<PageInfo<DeviceType>> DeviceTypeSearchList(Integer pageNum, Integer pageSize, String type, String searchText) {
        if (pageNum == null) {
            pageNum = 1;
        }
        if (pageSize == null) {
            pageSize = 5;
        }
        // 下次请求会带上分页数据
        PageHelper.startPage(pageNum, pageSize);
        List<DeviceType> list = deviceTypeService.search(type, searchText);
        // 通过这个api获取pageInfo
        PageInfo<DeviceType> page = PageInfo.of(list);
        return new AjaxResult<>(page);
    }

    //批量删除
    @GetMapping("/device/type/deleteAll")
    @ResponseBody
    public AjaxResult DeviceTypeDeleteAll(String ids){
        String[] idsarr = ids.split(",");
        int i =0;
        for(String s : idsarr){
            deviceTypeService.delete(s);
            i++;
        }
        if (i >= 1) {
            return new AjaxResult("删除成功");
        } else {
            return new AjaxResult("删除失败", 500);
        }
    }
}
